#!/bin/bash
#############################################
# author:fang
# version : v1.0
# name  : xlog
# dispcripe: uses of log
# CopyRight@fangyunjiang[42550564@qq.com]
#############################################

source ~/command/common

function cal_process_time()
{
	local list len i sum av t
	list=(`sed '/cost time in '$1' process/!d' $2|sed 's/^.*process \([0-9]\+\).*/\1/'`)
	len=${#list[@]}
	[ $len -eq 0 ]&&return
	sum=0
	for ((i=0; i<len; i++));do
		((sum+=list[i]))
	done
	av=`echo "scale=2;$sum/$len/1000"|bc`
	echo "cost time in $1 for $len times:"
	echo "average is $av ms"
}

function cal_average_interval()
{
	local list len i sum av t
	list=(`sed '/cost time in '$1' interval/!d' $2|sed '1d'|sed  's/^.*interval \([0-9]\+\).*/\1/'`)
	len=${#list[@]}
	[ $len -eq 0 ]&&return
	sum=0
	for ((i=0; i<len; i++));do
		((sum+=list[i]))
	done
	av=`echo "scale=2;$sum/$len/1000"|bc`
	echo "the interval in $1 for $len times:"
	echo "average is $av ms"
}


function cal_average_size()
{
	local list len i sum av bps
	list=(`sed '/send rtp/!d;s/^.*length=\([0-9]\+\).*/\1/' $1`)
	len=${#list[@]}
	[ $len -eq 0 ]&&return
	sum=0
	for ((i=0; i<len; i++));do
		((sum+=list[i]))
	done
	av=`echo "scale=2;$sum/$len"|bc`
	bps=`echo "scale=2;$av*15/1024"|bc`
	echo "send rtp len for $len times:"
	echo "average is $av"
	echo "bps is $bps K/s"
}

function cal_all_time()
{
	local i
	for((i=0; i<$2;i++));do
		cal_process_time $i $1
	done
}

function cal_all_interval()
{
	local i
	for((i=0; i<$2;i++));do
		cal_average_interval $i $1
	done
}

function cal_time_and_size()
{
	cal_all_time $1 4
	cal_average_size $1
}

function edit_with_vi()
{
	local file

	file=$1
	if [ $_edit -eq 1 ];then
		vi "$file"
		if [ $_remove -eq 1 ];then
			rm "$file"
		fi
	fi
}

function read_pbpaste_data_to_file()
{
	local file

	file=$1
	[ $_append -eq 0 ] && __backup_file "$file"
	[ -z "$file" ]&&return

  if __mac;then
    if [ $_append -eq 0 ];then
      pbpaste > "$file"
    else
      echo>>"$file"
      pbpaste >> "$file"
    fi
  elif __windows;then
    if [ $_append -eq 0 ];then
      cat /dev/clipboard > "$file"
    else
      echo>>"$file"
      cat /dev/clipboard >> "$file"
    fi
  fi

	edit_with_vi "$file"

	#echo "have put data to $file"
}

function log_for_android()
{
	local file

	#__backup_file "$file"
	file=$1
	[ -f $file ] && rm "$file"

	trap "edit_with_vi '$file';exit" INT

	adb logcat -c
	if [ -z "$_tag" ];then
		adb logcat|tee "$file"
	else
		adb logcat -s "$_tag"|tee "$file"
	fi
}


function show_diff_n()
{
	local files list1 list2 list3 list4 len num
	files=(`echo $1|tr ':' ' '|tr -s ' '`)
	len=${#files[@]}
	for ((i=0; i<len; i++));do
		[ -f "${files[i]}" ] || return
	done
	for ((i=0; i<len; i++));do
		eval `awk -v FS=";;;;" '{print "list'$i'["NR-1"]=\""$1"\""}' ${files[i]}`
	done
	num=${#list0[@]}
	for((i=0;i<num; i++));do
		for((j=0;j<len;j++));do
			eval "str=\${list$j[i]}"
			printf "%-35s" "$str"
		done
		printf "\n"
	done
}

function show_diff_2()
{
	local file1 file2 list1 list2 len i
	file1=${1%%:*}
	file2=${1##*:}

	[ -f "$file1" -a -f "$file2" ] || return
	[ "$file1" = "$file2" ] && return

	eval `awk -v FS=";;;;" '{print "list1["NR-1"]=\""$1"\""}' $file1`
	eval `awk -v FS=";;;;" '{print "list2["NR-1"]=\""$1"\""}' $file2`

	len=${#list1[@]}

	for((i=0;i<len;i++));do
		printf "%-40s%s\n" "${list1[i]}" "${list2[i]}"
	done
}

function show_help()
{
	echo "Usage:xlog v1.0: only used by longmaster video test"
	echo "  xlog [-f file][-n times][-d diff][-t][-s][-i][-r][-a][-v][-m][-h]"
	echo "  -f file:set log file"
	echo "  -n file:set cal times"
	echo "  -t:cal average time"
	echo "  -s:cal average size"
	echo "  -i:cal average interval"
	echo "  -r:read pbpaste data to file"
	echo "  -a:log for android"
	echo "  -x:连接逍遥模拟器"
	echo "  -v:edit with vi after paste[affect -r|-a]"
	echo "  -m:rm after edit[affect -r|-a -v]"
	echo "  -g tag:log for android's tag[affect -a]"
	echo "  -d diff:show file diff[like -d 1.txt:2.txt:3.txt]"
	echo
	echo "  CopyRight(c)yunjiang.fang@longmaster.com.cn"
}

_file="1.txt"
_times=1
_oper=0
_edit=0
_remove=0
_append=0
_tag=""

function main()
{
	while getopts :f:n:d:g:tsiraxvmph opt;do
		_oper_right=1
		case $opt in
			f)_file="$OPTARG";;
			n)_times="$OPTARG";;
			t)_oper=1;;
			s)_oper=2;;
			i)_oper=3;;
			r)_oper=4;;
			a)_oper=5;;
			x)adb connect 127.0.0.1:21503;exit;;
			v)_edit=1;;
			m)_remove=1;;
			p)_append=1;;
			d)_oper=6;_diff="$OPTARG";;
			g)_tag="$OPTARG";;
			h)show_help;exit;;
			*)show_help;exit;;
		esac
	done

	case $_oper in
		0)cal_time_and_size "$_file";;
		1)cal_all_time "$_file" "$_times";;
		2)cal_average_size "$_file";;
		3)cal_all_interval "$_file" "$_times";;
		4)read_pbpaste_data_to_file "$_file";;
		5)log_for_android "$_file";;
		6)show_diff_n "$_diff";;
	esac
}

main "$@"
